/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package infbmobile.comunicaciones;

import infbmobile.main.InfBMobile;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import infbmobile.almacenamiento.*;

/**
 *
 * @author inftel
 */
public class ConexionPedirCita {

    private HttpConnection conexion;
    private Vector horas;
    private OutputStream salida;
    private String fecha;
    private InputStream entrada;
    private String hora;

    public Vector getHoras() {
        return horas;
    }
    public void CrearCita(){
        try {
            salida = conexion.openOutputStream();
            DataOutputStream out = new DataOutputStream(salida);
            out.writeUTF("crear");
            out.writeUTF(fecha);
            out.writeUTF(hora);
            out.close();
            salida.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public ConexionPedirCita(InfBMobile midlet,String fecha,String hora) {
        this.hora=hora;
       
        this.fecha = fecha;
        try {
            Identificacion.cargarIdSesion();
            String idSesion=Identificacion.getIdSesion();
            String miUrl = "http://" +  midlet.servidorWeb + ":" + midlet.puerto + "/GestionHospitalaria/Citashorasdisponible;jsessionid="+idSesion;
            conexion = (HttpConnection) Connector.open(miUrl);
            conexion.setRequestProperty("Content-Language", "es-ES");
            conexion.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
            conexion.setRequestProperty("Connection", "close");
            conexion.setRequestMethod(HttpConnection.GET);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public Vector  Sacarhoras() {
        try {
            salida = conexion.openOutputStream();
            
            DataOutputStream out = new DataOutputStream(salida);
            out.writeUTF("pedir horas");
            out.writeUTF(fecha);
            out.flush();
            out.close();
            salida.close();

            entrada= conexion.openInputStream();
            DataInputStream in = new DataInputStream(entrada);
            horas = new Vector();
            int size = in.readInt();
            
            for (int i = 0; i < size; i++) {
                String hr = in.readUTF();
                horas.addElement(hr);
            }

            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    return horas;
    }
}
